using System;
using System.Text;

namespace org.lionsoul.ip2region.test
{



	/// <summary>
	/// data check class
	/// 
	/// @author koma<komazhang@foxmail.com>
	/// 
	/// </summary>

	public class TestUnit
	{

		public static void Main(string[] args)
		{
			try
			{
				org.lionsoul.ip2region.DbSearcher _searcher = new org.lionsoul.ip2region.DbSearcher(new org.lionsoul.ip2region.DbConfig(), "./data/ip2region.db");
				java.io.BufferedReader bfr = new java.io.BufferedReader(new java.io.FileReader("./data/ip.merge.txt"));
				java.io.BufferedWriter bwr = new java.io.BufferedWriter(new java.io.FileWriter("./data/error_log.txt", true));
				int errCount = 0;
				int lineCount = 0;
				string str = null;

				while ((str = bfr.readLine()) != null)
				{
					StringBuilder line = new StringBuilder(str);
					//get first ip
					int first_idx = line.IndexOf("|");
					string first_ip = line.Substring(0, first_idx);

					line = new StringBuilder(line.Substring(first_idx + 1));

					//get second ip
					int second_idx = line.IndexOf("|");
					string second_ip = line.Substring(0, second_idx);

					//get addr
					string source_region = line.Substring(second_idx + 1);

					//search from DbSearcher
					Console.WriteLine("+---Start, start to search");
					Console.WriteLine("+---[Info]: Source region = " + source_region);

					Console.WriteLine("+---[Info]: Step1, search for first IP: " + first_ip);
					org.lionsoul.ip2region.DataBlock fdata = _searcher.binarySearch(first_ip);
					if (!fdata.Region.equalsIgnoreCase(source_region))
					{
						Console.WriteLine("[Error]: Search first IP failed, DB region = " + fdata.Region);
						bwr.write("[Source]: Region: " + fdata.Region);
						bwr.newLine();
						bwr.write("[Source]: First Ip: " + first_ip);
						bwr.newLine();
						bwr.write("[DB]: Region: " + fdata.Region);
						bwr.newLine();
						bwr.flush();
						errCount++;
					}

					Console.WriteLine("+---[Info]: Step2, search for second IP: " + second_ip);
					org.lionsoul.ip2region.DataBlock sdata = _searcher.btreeSearch(second_ip);
					if (!sdata.Region.equalsIgnoreCase(source_region))
					{
						Console.WriteLine("[Error]: Search second IP failed, DB region = " + sdata.Region);
						bwr.write("[Source]: Region: " + sdata.Region);
						bwr.newLine();
						bwr.write("[Source]: First Ip: " + second_ip);
						bwr.newLine();
						bwr.write("[DB]: Region: " + sdata.Region);
						bwr.newLine();
						bwr.flush();
						errCount++;
					}

					lineCount++;
				}

				bwr.close();
				bfr.close();
				Console.WriteLine("+---Done, search complished");
				Console.WriteLine("+---Statistics, Error count = " + errCount + ", Total line = " + lineCount + ", Fail ratio = " + ((float)(errCount / lineCount)) * 100 + "%");
			}
			catch (java.io.FileNotFoundException e)
			{
				Console.WriteLine(e.ToString());
				Console.Write(e.StackTrace);
			}
			catch (org.lionsoul.ip2region.DbMakerConfigException e)
			{
				Console.WriteLine(e.ToString());
				Console.Write(e.StackTrace);
			}
			catch (Exception e)
			{
				Console.WriteLine(e.ToString());
				Console.Write(e.StackTrace);
			}
		}

	}

}